import random


class Minesweeper:

    def __init__(self, height=8, width=8, mines=8):
        # set initialize width, height and mine count
        self.height = height
        self.width = width
        self.mines = set()
        self.mines_found = set()

        # initialize no mine field
        self.board = []
        for i in range(self.height):
            row = []
            for j in range(self.width):
                row.append(False)
            self.board.append(row)

        # random add mine
        while len(self.mines) != mines:
            i = random.randrange(height)
            j = random.randrange(width)
            if not self.board[i][j]:
                self.mines.add((i, j))
                self.board[i][j] = True

    def print(self):
        for i in range(self.height):
            print("--" * self.widht + "-")

            for j in range(self.width):
                if self.board[i][j]:
                    print(" |X", end="")
                else:
                    print("| ", end="")
            print("--" * self.width + "-")

    def is_mine(self, cell):
        i, j = cell
        return self.board[i][j]

    def nearby_mines(self, cell):
        # keep mine count nearby
        count = 0

        # iterator all cell in line and row
        for i in range(cell[0] - 1, cell[0] + 2):
            for j in range(cell[1] - 1, cell[1] + 2):
                # skip cell self
                if(i, j) == cell:
                    continue

                # if mine beside cell, update count
                if 0 <= i < self.height and 0 <= j < self.width:
                    if self.board[i][j]:
                        count += 1
        return count

    def won(self):
        return self.mines_found == self.mines
